/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog;

import com.googlecode.jaden.common.other.Utils;
import static com.googlecode.jaden.ui.Constants.*;
import com.googlecode.jaden.ui.UI;
import com.googlecode.jaden.ui.UiUtils;

import javax.swing.*;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class PreferencesDialog extends BaseDialog {

    private final Preferences preferences;
    private final JCheckBox reloadLastProjectCB;
    private final JCheckBox warnModifiedCB;
    private final JCheckBox showSplashCB;
    private final SpinnerNumberModel reopenMaxSizeSpinnerModel;
    private final JCheckBox confirmOnCloseCB;
    private final JCheckBox showToolbarCB;

    public PreferencesDialog(UI ui, Preferences preferences) {
        super(ui, DIALOG_PREFERENCES_TITLE);
        this.preferences = preferences;

        reloadLastProjectCB = new JCheckBox(Utils.translate(LABEL_RELOAD_LAST_TEXT));
        reloadLastProjectCB.setSelected(preferences.getBoolean(UI_LOAD_LAST, UI_LOAD_LAST_DEFAULT));
        warnModifiedCB = new JCheckBox(Utils.translate(LABEL_WARN_MODIFIED_TEXT));
        warnModifiedCB.setSelected(preferences.getBoolean(UI_WARN_MODIFIED, UI_WARN_MODIFIED_DEFAULT));
        showSplashCB = new JCheckBox(Utils.translate(LABEL_SHOW_SPLASH_TEXT));
        showSplashCB.setSelected(preferences.getBoolean(UI_SHOW_SPLASH, UI_SHOW_SPLASH_DEFAULT));
        showToolbarCB = new JCheckBox(Utils.translate(LABEL_SHOW_TOOLBAR_TEXT));
        showToolbarCB.setSelected(preferences.getBoolean(UI_SHOW_TOOLBAR, UI_SHOW_TOOLBAR_DEFAULT));
        reopenMaxSizeSpinnerModel = new SpinnerNumberModel(4, 1, 9, 1);
        reopenMaxSizeSpinnerModel.setValue(preferences.getInt(UI_NUMBER_OF_FILES,
                UI_NUMBER_OF_FILES_DEFAULT));
        JSpinner reopenMaxSizeSpinner = new JSpinner(reopenMaxSizeSpinnerModel);
        confirmOnCloseCB = new JCheckBox(Utils.translate(LABEL_CONFIRM_ON_CLOSE_TEXT));
        confirmOnCloseCB.setSelected(preferences.getBoolean(UI_CONFIRM_ON_CLOSE, UI_CONFIRM_ON_CLOSE_DEFAULT));
        JPanel mainPanel = UiUtils.verticalList(UiUtils.horizontalList(reloadLastProjectCB),
                UiUtils.horizontalList(warnModifiedCB), UiUtils.horizontalList(showSplashCB),
                UiUtils.horizontalList(new JLabel(Utils.translate(LABEL_NUMBER_OF_RECENT_FILES_TEXT)),
                        reopenMaxSizeSpinner),
                UiUtils.horizontalList(confirmOnCloseCB),
                UiUtils.horizontalList(showToolbarCB)
                );
        setMainPanel(mainPanel, preferences);
    }

    protected void doOk() {
        preferences.putBoolean(UI_LOAD_LAST, reloadLastProjectCB.isSelected());
        preferences.putBoolean(UI_WARN_MODIFIED, warnModifiedCB.isSelected());
        preferences.putBoolean(UI_SHOW_SPLASH, showSplashCB.isSelected());
        preferences.putInt(UI_NUMBER_OF_FILES, reopenMaxSizeSpinnerModel.getNumber().intValue());
        preferences.putBoolean(UI_CONFIRM_ON_CLOSE, confirmOnCloseCB.isSelected());
        preferences.putBoolean(UI_SHOW_TOOLBAR, showToolbarCB.isSelected());
        ((UI) getOwner()).setMaxReopenFiles(reopenMaxSizeSpinnerModel.getNumber().intValue());
        try {
            preferences.flush();
        } catch (BackingStoreException e) {
            Utils.handleException(e);
        }
        dispose();
    }
}